package android.slc.appbase.ui.widget.grid;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import javafx.collections.ObservableList;
import javafx.scene.Node;
import javafx.scene.layout.GridPane;

public class GridPaneUtils {

    public static void formatGridColumn(GridPane gridPane, int columnCount) {
        formatGridColumn(gridPane, columnCount, new OnFormatColumnConsumer() {
            private int rowIndex = -1;

            @Override
            public void accept(Node value, int columnCount, int index) {
                int columnIndex = index % columnCount;
                if (columnIndex == 0) {
                    rowIndex++;
                }
                GridPane.setConstraints(value, columnIndex, rowIndex);
            }
        });
    }

    public static void formatGridColumn(GridPane gridPane, int columnCount, OnFormatColumnConsumer onFormatColumnConsumer) {
        ObservableList<Node> nodeList = gridPane.getChildren();
        CollectionUtil.forEach(nodeList, new CollUtil.Consumer<Node>() {

            @Override
            public void accept(Node value, int index) {
                onFormatColumnConsumer.accept(value, columnCount, index);
            }
        });
    }

    public interface OnFormatColumnConsumer {
        public void accept(Node value, int columnCount, int index);
    }
}
